
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.research3zxtce.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.research3zxtce.entity.Research3zxtce;
import com.application.research3zxtce.struts.form.Research3zxtceForm;
import com.application.usertable.entity.Usertable;
import com.application.yfproject.entity.YfProject;
import com.manager.BaseDao;
import com.manager.commons.CommonUtil;
import com.manager.commons.CurrentUserUtils;
import com.manager.commons.DateUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:Research3zxtceDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 左磊 QQ：30575951 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class Research3zxtceDAO extends HibernateGenericDao<Research3zxtce> {
	private static final Logger logger = Logger
			.getLogger(Research3zxtceDAO.class);

	private Research3zxtce research3zxtce;

	public Research3zxtce getResearch3zxtce() {
		return this.research3zxtce;
	}

	public void setResearch3zxtce(Research3zxtce research3zxtce) {
		this.research3zxtce = research3zxtce;
	}

	public Integer getAllCountResearch3zxtce(
			Research3zxtceForm research3zxtceForm) {
		// 储存查询条件的buffer
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();

		buf
				.append("SELECT COUNT(0) FROM research3zxtce t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		// 查询字符串
		buf = getStringBuffer(research3zxtceForm, buf, args);
		return super.getCount(buf.toString(), args.toArray());
	}

	public void deleteResearch3zxtces(Research3zxtceForm research3zxtceForm) {
		String a = research3zxtceForm.getIds().replaceAll(",", "','");
		String hql = " delete from Research3zxtce where id in  ( '" + a + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteResearch3zxtces failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchResearch3zxtce(
			final Research3zxtceForm research3zxtceForm, final PageBean pageBean) {

		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,u.rname as uname,p.project_org ,p.project_name,b.name as orgname FROM research3zxtce t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		buf = getStringBuffer(research3zxtceForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(pageBean, buf.toString(), args);
	}

	public List searchResearch3zxtce(final Research3zxtceForm research3zxtceForm) {
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,u.rname as uname,p.project_org ,p.project_name,b.name as orgname   FROM research3zxtce t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		buf = getStringBuffer(research3zxtceForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(null, buf.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param research3zxtceForm
	 * @return
	 */
	private StringBuffer getStringBuffer(Research3zxtceForm research3zxtceForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */
		if (StringUtils.isNotBlank(research3zxtceForm.getId())) {
			buf.append(" and t.id = '" + research3zxtceForm.getId().trim()
					+ "'");
		} else { 
			if (StringUtils.isNotBlank(research3zxtceForm.getProjectNo())) {
				buf.append(" and t.project_no = '"
						+ research3zxtceForm.getProjectNo().trim() + "'");
			}
			if (StringUtils.isNotBlank(research3zxtceForm.getProjectOrg())) {
				buf.append(" and p.project_org = '"
						+ research3zxtceForm.getProjectOrg().trim() + "'");
			}
			if (StringUtils.isNotBlank(research3zxtceForm.getProjectName())) {
				buf.append(" and p.project_name like '%"
						+ research3zxtceForm.getProjectName().trim() + "%'");
			}
			if (StringUtils.isNotBlank(research3zxtceForm.getUname())) {
				buf.append(" and u.rname like '%"
						+ research3zxtceForm.getUname().trim() + "%'");
			}
			if (StringUtils.isNotBlank(research3zxtceForm.getOperateid())) {
				buf.append(" and t.operateid = '"
						+ research3zxtceForm.getOperateid() + "'");
			}
			if (StringUtils.isNotBlank(research3zxtceForm.getCreateDateState())) {
				buf.append(" and t.createDate > '"
						+ research3zxtceForm.getCreateDateState() + "'");
			}
			if (StringUtils.isNotBlank(research3zxtceForm.getCreateDateState())) {
				buf.append(" and t.createDate < '"
						+ research3zxtceForm.getCreateDateEnd() + "'");
			}
		}
		buf = CommonUtil.filterData(buf, args);
		return buf;
	}
}
